package org.hcf.api.tencent.callback;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.hcf.api.base.Const;
import org.hcf.api.base.Helper;
import org.hcf.api.base.SnowflakeUtils;
import org.hcf.api.tencent.callback.info.FriendChatMsgInfo;
import org.hcf.api.tencent.callback.info.content.TEXT;
import org.hcf.api.intf.ICallBack;
import org.hcf.api.tencent.callback.info.group.base.MessageBody;

@Slf4j
@Data
/** 发送好友消息回调 */
public class CallbackFriendMsgHandler implements ICallBack {
    @Override
    public String getCommand() {
        return Const.CMD.FRIEND_SEND_MSG_AFTER;// "C2C.CallbackAfterSendMsg";
    }

    @Override
    public JSONObject action(JSONObject data) {
        log.info("准备处理发送单聊消息回调信息:" + data);
        FriendChatMsgInfo info = data.toJavaObject(FriendChatMsgInfo.class);
        log.info("解单聊回调消息:" + JSONObject.toJSON(info));
        if (!getCommand().equals(info.getCallbackCommand())) {
            log.error("单聊回调消息中[" + data.getString("CallbackCommand") + "]命令不匹配");
//            return Helper.toSuccess();
        }
        long msgId = SnowflakeUtils.nextId();
        for (MessageBody messageBody : info.getMsgBody()) {
            if (messageBody.getMsgType().equals(Const.MSG_TYPE.TEXT)) {
                if (info.getSendMsgResult() == 0) {
                    long from = Long.parseLong(info.getFrom_Account());
                    long to = Long.parseLong(info.getTo_Account());
                    TEXT text = JSONObject.parseObject(messageBody.getMsgContent().toString(), TEXT.class);
                    Helper.sendToPlugin(from, to, String.valueOf(to), text.getText(), msgId);
                } else
                    log.error("好友消息[" + info.getMsgSeq() + "," + Const.MSG_TYPE.TEXT + "]错误[" + info.getSendMsgResult() + ":" + info.getErrorInfo() + "]不处理");
            } else log.error("好友消息[" + info.getMsgSeq() + "]类型[" + messageBody.getMsgType() + "]不处理");

        }
        return Helper.toSuccess();
    }
}
